﻿using System;
using System.Data;
using System.Text;
using System.Data.OracleClient;
using DBUtility;//Please add references
namespace DataAccess
{
    /// <summary>
    /// 数据访问类:TB_USER
    /// </summary>
    public partial class TB_USER
    {
        public TB_USER()
        { }
        #region  Method
        public static int GetCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) num ");
            strSql.Append(" FROM TB_USER ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperOra.GetSingle(strSql.ToString());
            return int.Parse(obj.ToString());
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(string CUSERNAME)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from TB_USER");
            strSql.Append(" where CUSERNAME=:CUSERNAME ");
            OracleParameter[] parameters = {
										new OracleParameter(":CUSERNAME", OracleType.NVarChar)};

            parameters[0].Value = CUSERNAME;

            return DbHelperOra.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static bool Add(Model.TB_USER model)
        {
            model.NID = CommonExecute.GetID(model.GetType().Name);

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into TB_USER(");
            strSql.Append("NID,CUSERNAME,CNICKNAME,CPASSWORD,NTYPE)");
            strSql.Append(" values (");
            strSql.Append(":NID,:CUSERNAME,:CNICKNAME,:CPASSWORD,:NTYPE)");
            OracleParameter[] parameters = {
					new OracleParameter(":NID", OracleType.Number,10),
					new OracleParameter(":CUSERNAME", OracleType.NVarChar),
					new OracleParameter(":CNICKNAME", OracleType.NVarChar),
					new OracleParameter(":CPASSWORD", OracleType.NVarChar),
					new OracleParameter(":NTYPE", OracleType.Number,1)};
            parameters[0].Value = model.NID;
            parameters[1].Value = model.CUSERNAME;
            parameters[2].Value = model.CNICKNAME;
            parameters[3].Value = model.CPASSWORD;
            parameters[4].Value = model.NTYPE;

            int rows = DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static bool Update(Model.TB_USER model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update TB_USER set ");
            strSql.Append("CNICKNAME=:CNICKNAME,");
            strSql.Append("CPASSWORD=:CPASSWORD,");
            strSql.Append("NTYPE=:NTYPE,");
            strSql.Append("CUSERNAME=:CUSERNAME");
            strSql.Append(" where NID=:NID ");
            OracleParameter[] parameters = {
					new OracleParameter(":CNICKNAME", OracleType.NVarChar),
					new OracleParameter(":CPASSWORD", OracleType.NVarChar),
					new OracleParameter(":NTYPE", OracleType.Number,1),
					new OracleParameter(":NID", OracleType.Number,10),
					new OracleParameter(":CUSERNAME", OracleType.NVarChar)};
            parameters[0].Value = model.CNICKNAME;
            parameters[1].Value = model.CPASSWORD;
            parameters[2].Value = model.NTYPE;
            parameters[3].Value = model.NID;
            parameters[4].Value = model.CUSERNAME;

            int rows = DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static bool Delete(decimal NID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from TB_USER ");
            strSql.Append(" where NID=:NID");
            OracleParameter[] parameters = {
					new OracleParameter(":NID", OracleType.Number,22)};
            parameters[0].Value = NID;

            int rows = DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Model.TB_USER GetModel(decimal NID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select NID,CUSERNAME,CNICKNAME,CPASSWORD,NTYPE from TB_USER ");
            strSql.Append(" where NID=:NID ");
            OracleParameter[] parameters = {
					new OracleParameter(":NID", OracleType.Number,22)};
            parameters[0].Value = NID;

            Model.TB_USER model = new Model.TB_USER();
            DataSet ds = DbHelperOra.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["NID"] != null && ds.Tables[0].Rows[0]["NID"].ToString() != "")
                {
                    model.NID = decimal.Parse(ds.Tables[0].Rows[0]["NID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CUSERNAME"] != null && ds.Tables[0].Rows[0]["CUSERNAME"].ToString() != "")
                {
                    model.CUSERNAME = ds.Tables[0].Rows[0]["CUSERNAME"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CNICKNAME"] != null && ds.Tables[0].Rows[0]["CNICKNAME"].ToString() != "")
                {
                    model.CNICKNAME = ds.Tables[0].Rows[0]["CNICKNAME"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CPASSWORD"] != null && ds.Tables[0].Rows[0]["CPASSWORD"].ToString() != "")
                {
                    model.CPASSWORD = ds.Tables[0].Rows[0]["CPASSWORD"].ToString();
                }
                if (ds.Tables[0].Rows[0]["NTYPE"] != null && ds.Tables[0].Rows[0]["NTYPE"].ToString() != "")
                {
                    model.NTYPE = decimal.Parse(ds.Tables[0].Rows[0]["NTYPE"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Model.TB_USER GetModel(string CUSERNAME)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select NID,CUSERNAME,CNICKNAME,CPASSWORD,NTYPE from TB_USER ");
            strSql.Append(" where CUSERNAME=:CUSERNAME ");
            OracleParameter[] parameters = {
					new OracleParameter(":CUSERNAME", OracleType.VarChar)};
            parameters[0].Value = CUSERNAME;

            Model.TB_USER model = new Model.TB_USER();
            DataSet ds = DbHelperOra.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["NID"] != null && ds.Tables[0].Rows[0]["NID"].ToString() != "")
                {
                    model.NID = decimal.Parse(ds.Tables[0].Rows[0]["NID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CUSERNAME"] != null && ds.Tables[0].Rows[0]["CUSERNAME"].ToString() != "")
                {
                    model.CUSERNAME = ds.Tables[0].Rows[0]["CUSERNAME"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CNICKNAME"] != null && ds.Tables[0].Rows[0]["CNICKNAME"].ToString() != "")
                {
                    model.CNICKNAME = ds.Tables[0].Rows[0]["CNICKNAME"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CPASSWORD"] != null && ds.Tables[0].Rows[0]["CPASSWORD"].ToString() != "")
                {
                    model.CPASSWORD = ds.Tables[0].Rows[0]["CPASSWORD"].ToString();
                }
                if (ds.Tables[0].Rows[0]["NTYPE"] != null && ds.Tables[0].Rows[0]["NTYPE"].ToString() != "")
                {
                    model.NTYPE = decimal.Parse(ds.Tables[0].Rows[0]["NTYPE"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select NID,CUSERNAME,CNICKNAME,CPASSWORD,NTYPE ");
            strSql.Append(" FROM TB_USER ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOra.Query(strSql.ToString());
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataTable GetList(string strWhere, int startRecord, int maxRecords)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM TB_USER ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            return DbHelperOra.Query(strSql.ToString(), startRecord, maxRecords);
        }
        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            OracleParameter[] parameters = {
                    new OracleParameter(":tblName", OracleType.VarChar, 255),
                    new OracleParameter(":fldName", OracleType.VarChar, 255),
                    new OracleParameter(":PageSize", OracleType.Number),
                    new OracleParameter(":PageIndex", OracleType.Number),
                    new OracleParameter(":IsReCount", OracleType.Clob),
                    new OracleParameter(":OrderType", OracleType.Clob),
                    new OracleParameter(":strWhere", OracleType.VarChar,1000),
                    };
            parameters[0].Value = "TB_USER";
            parameters[1].Value = "CUSERNAME";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperOra.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

